/*
 * Copyright (C) 2009, 2010, B3log Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.b3log.nide.servlet;

import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletRequestEvent;
import javax.servlet.http.HttpSessionEvent;
import org.b3log.latke.Latkes;
import org.b3log.latke.RunsOnEnv;
import org.b3log.latke.servlet.AbstractServletListener;
import org.b3log.nide.Nide;
import org.b3log.nide.jsonrpc.CompileService;
import org.b3log.nide.jsonrpc.MavenService;
import org.b3log.nide.jsonrpc.ProjectService;
import org.jabsorb.JSONRPCBridge;

/**
 * Nide servlet listener.
 *
 * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
 * @version 1.0.0.8, Nov 29, 2010
 */
public final class NideServletListener extends AbstractServletListener {

    /**
     * Logger.
     */
    private static final Logger LOGGER =
            Logger.getLogger(NideServletListener.class.getName());

    @Override
    public void contextInitialized(final ServletContextEvent servletContextEvent) {
        Latkes.setRunsOnEnv(RunsOnEnv.LOCAL);
        LOGGER.info("Latke is running on local.");
        final String sleepyEnvPath =
                Nide.getNideProperties().getString("sleepycat.env.path");
        Latkes.setRepositoryPath(sleepyEnvPath);
        LOGGER.log(Level.INFO, "Initialized sleepycat envionment path[{0}]",
                   sleepyEnvPath);

        super.contextInitialized(servletContextEvent);

        Latkes.setDefaultLocale(Locale.US);


        registerRemoteJSServices();
        LOGGER.info("Initialized the context");
    }

    @Override
    public void contextDestroyed(final ServletContextEvent servletContextEvent) {
        super.contextDestroyed(servletContextEvent);
        LOGGER.info("Destroyed the context");
    }

    @Override
    public void sessionCreated(final HttpSessionEvent httpSessionEvent) {
    }

    @Override
    public void sessionDestroyed(final HttpSessionEvent httpSessionEvent) {
    }

    @Override
    public void requestInitialized(final ServletRequestEvent servletRequestEvent) {
    }

    @Override
    public void requestDestroyed(final ServletRequestEvent servletRequestEvent) {
    }

    /**
     * Registers remote JavaScript services.
     */
    private void registerRemoteJSServices() {
        final JSONRPCBridge jsonRpcBridge = JSONRPCBridge.getGlobalBridge();

        try {
            final CompileService compileService = new CompileService();
            jsonRpcBridge.registerObject(compileService.getServiceObjectName(),
                                         compileService);

            final MavenService mavenService = new MavenService();
            jsonRpcBridge.registerObject(mavenService.getServiceObjectName(),
                                         mavenService);

            final ProjectService projectService = new ProjectService();
            jsonRpcBridge.registerObject(projectService.getServiceObjectName(),
                                         projectService);
        } catch (final Exception e) {
            LOGGER.log(Level.SEVERE, e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }
}
